﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Auction_Web
{
    public partial class postmsg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Form.Count > 0)
            {
                if (Request.Form["type"] == "send")
                {
                    string user = Request.Form["user"].Trim();
                    string message = Request.Form["message"].Trim();
                    SendToAll(user, message);//发送消息给所有在线的人
                    Response.ContentType = "application/json";
                    Response.Write("{user:\"" + user + "\"}");
                }
            }
        }

        private void SendToAll(string source, string message)
        {
            Hashtable _copy = (Hashtable)globalCache.userCache.Clone();
            foreach (DictionaryEntry data in _copy)
            {
                if ((string)data.Key != source)
                {
                    lock (data.Value)
                    {
                        ((ChatTask)data.Value)._lastMessage = message;
                        ((ChatTask)data.Value)._lastSource = source;
                        ((ChatTask)data.Value).waitEvent.Set();//让对应的挂起的请求继续执行，即释放掉阻塞
                    }
                }
            }
        }
    }
}
